import store from "../store/index";
import { Toast } from "vant";

console.log('环境',process.env);
console.log('环境',process.env.VUE_APP_URL);

import axios from "axios";
const service = axios.create({
  // baseURL: process.env.VUE_APP_URL,
  baseURL: 'http://huruqing.cn:3007',
  timeout: 30000,
});

// 配置请求拦截
service.interceptors.request.use(
  (config) => {
    // Toast.loading({ forbidClick: true })
    // 添加token
    console.log(store.state.token);
    // config.headers["user-token"] = store.state.token;
    // config.headers["user-token"] = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI2MGQ2ZjBiMDhmMTE5ZDNlMjVhZjhjZWYiLCJwaG9uZSI6IjEzODExMTEyMjIyIiwiaWF0IjoxNjQ2NzkyMDk2LCJleHAiOjE2NTAzOTIwOTZ9.GtR3MWOWkSRvKhOT2uB04J8rEQEEythX6Ee6iWY0AiM';
    return config;
  },
  () => {
    Toast.fail("发送请求失败");
    Toast.clear();
  }
);

// 配置响应拦截
service.interceptors.response.use(
  (res) => {
    Toast.clear();
    if (res.data.code == 666) {
      return res.data;
    } else {
      Toast.fail(res.data.msg);
    }
  },
  () => {
    Toast.fail("网络异常,请稍后再试");
    Toast.clear();
  }
);

const get = (url, data) => {
  return service.get(url, { params: data });
};

export default {
  ...service,
  get,
};
